import {
  getGoodsStock,
  getInOrOutStoreGoods,
  getInStoreCodeList,
  getOutboundDetail,
  getOutboundMaterial,
  getOutStoreReceiptsUser,
  outboundList,
  outStoreTypePageSelect,
  rejectOutbound,
  saveOrCheck,
  storeEmpAddressPageSelect,
  storePageSelect,
  storePositionPageSelect,
  warehouseOutWriteBack,
} from '@/api/store'
import moment from 'moment'
import { ref } from 'vue'

/**
 * 出库表单服务
 */
export const useWarehouseOutForm = () => {
  // 表单保存加载
  const formSaveLoading = ref(false)

  // 出库表单
  const form = ref<StorageAPIRequest.WarehouseOutFormSave>({
    createTime: moment(new Date()).format('YYYY-MM-DD HH:mm'),
    delOutBoundMaterialIds: [],
    estimateTime: '',
    id: '',
    outBoundMaterialVos: [],
    receiptVo: {
      receiptAddrType: 2,
      receiptAddress: '',
      receiptArea: '',
      receiptCity: '',
      receiptName: '',
      receiptPhone: '',
      receiptProvince: '',
      receiptTown: '',
      relationId: '',
      relationName: '',
      relationNo: '',
      storePositionId: '',
    },
    remark: '',
    reviewRemark: '',
    source: 1,
    storeType: 0,
    typeId: '',
    type: 2,
  })

  /**
   * 表单重置
   */
  const resetForm = () => {
    form.value = {
      createTime: moment(new Date()).format('YYYY-MM-DD HH:mm'),
      delOutBoundMaterialIds: [],
      estimateTime: '',
      id: '',
      outBoundMaterialVos: [],
      receiptVo: {
        receiptAddrType: 2,
        receiptAddress: '',
        receiptArea: '',
        receiptCity: '',
        receiptName: '',
        receiptPhone: '',
        receiptProvince: '',
        receiptTown: '',
        relationId: '',
        relationName: '',
        relationNo: '',
        storePositionId: '',
      },
      remark: '',
      reviewRemark: '',
      source: 1,
      storeType: 0,
      type: 2,
      typeId: '',
    }
  }

  /**
   * 创建出库
   */
  const warehouseOutCreate = async () => {
    formSaveLoading.value = true
    const res = await saveOrCheck(form.value, 1)
    formSaveLoading.value = false
    return res.data
  }

  /**
   * 审核出库
   */
  const warehouseOutCheck = async () => {
    formSaveLoading.value = true
    const res = await saveOrCheck(form.value, 2)
    formSaveLoading.value = false
    return res.data
  }

  return {
    form,
    formSaveLoading,
    resetForm,
    warehouseOutCheck,
    warehouseOutCreate,
  }
}

/**
 * 获取出库类型下拉选项
 */
export const useGetWarehouseOutTypeOptions = () => {
  // 获取出库类型下拉选项加载
  const getOptionsLoading = ref(false)

  // 获取出库类型下拉选项参数
  const getOptionsParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  // 出库类型下拉选项
  const options = ref<Array<WarehouseOutTypeOption>>([])

  /**
   * 获取下拉选项
   */
  const getOptions = async () => {
    getOptionsLoading.value = true
    const res = await outStoreTypePageSelect(getOptionsParam.value)
    options.value = res.data.data.rows
    getOptionsLoading.value = false
  }

  return {
    options,
    getOptionsParam,
    getOptionsLoading,
    getOptions,
  }
}

/**
 * 获取出库收货公司信息
 */
export const useGetWarehouseOutReceiptCustomerList = () => {
  // 获取公司信息加载
  const getCustomerListLoading = ref(false)

  // 获取公司信息请求参数
  const getCustomerListParam = ref({
    page: 1,
    limit: 50,
    name: '',
  })

  // 公司列表
  const customerList = ref<Array<WarehouseOutReceiptCustomerInfo>>([])

  const getCustomer = async () => {
    getCustomerListLoading.value = true
    const res = await getOutStoreReceiptsUser(getCustomerListParam.value)
    customerList.value = res.data.data.rows
    getCustomerListLoading.value = false
  }

  return {
    customerList,
    getCustomerListParam,
    getCustomerListLoading,
    getCustomer,
  }
}

/**
 * 获取仓库及人员地址列表
 */
export const useGetWarehouseAndPersonAddressList = () => {
  // 列表
  const list = ref<Array<WarehouseOutWarehouseAndPersonAddress>>([])

  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref({
    name: '',
    limit: 1000,
    page: 1,
    id: '',
  })

  /**
   * 获取列表
   */
  const getList = async (storeType: 0 | 1 | 2) => {
    getListLoading.value = true
    const res = await storeEmpAddressPageSelect(getListParam.value, storeType)
    list.value = res.data.data.rows
    getListLoading.value = false
  }

  return {
    list,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 获取出库产品列表
 */
export const useGetWarehouseOutGoodList = () => {
  // 获取产品列表加载
  const getGoodListLoading = ref(false)

  // 获取产品列表请求参数
  const getGoodListParam = ref<StorageAPIRequest.GetWarehouseOutGoodList>({
    brandId: '',
    keyword: '',
    limit: 50,
    page: 1,
    typeId: '',
    typeThreeId: '',
    typeTwoId: '',
  })

  // 列表总条数
  const listTotal = ref(0)

  // 产品列表
  const goodList = ref<Array<WarehouseOutGood>>([])

  /**
   * 获取产品列表
   * @param {(1|2)} goodType - 产品类型：1-成品，2-物料
   */
  const getGoodList = async (goodType: 1 | 2) => {
    getGoodListLoading.value = true
    const res = await getInOrOutStoreGoods(getGoodListParam.value, goodType)
    goodList.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getGoodListLoading.value = false
  }

  return {
    goodList,
    listTotal,
    getGoodListParam,
    getGoodListLoading,
    getGoodList,
  }
}

/**
 * 获取仓库下拉选项
 */
export const useGetWarehouseOptionList = () => {
  // 获取下拉选项加载
  const getWarehouseOptionListLoading = ref(false)

  // 获取下拉选项请求参数
  const getWarehouseOptionListParam = ref({
    name: '',
    limit: 1000,
    page: 1,
    id: '',
  })

  // 仓库下拉选项
  const warehouseOptionList = ref<Array<WarehouseOption>>([])

  /**
   * 获取仓库下拉选项
   */
  const getWarehouseOptionList = async (warehouseType: 0 | 1 | 2) => {
    getWarehouseOptionListLoading.value = true
    const res = await storePageSelect(getWarehouseOptionListParam.value, warehouseType)
    warehouseOptionList.value = res.data.data.rows
    getWarehouseOptionListLoading.value = false
  }

  return {
    warehouseOptionList,
    getWarehouseOptionListParam,
    getWarehouseOptionListLoading,
    getWarehouseOptionList,
  }
}

/**
 * 获取仓位下拉选项
 */
export const useGetWarehousePositionOptionList = () => {
  // 获取仓位下拉选项加载
  const getWarehousePositionOptionListLoaing = ref(false)

  // 获取仓位下拉选项请求参数
  const getWarehousePositionOptionListParam = ref({
    name: '',
    limit: 0,
    page: 0,
  })

  // 仓位下拉选项
  const warehousePositionOptionList = ref<Array<WarehousePositionOption>>([])

  /**
   * 获取仓位下拉选项
   * @param {String} warehouseId - 仓库id
   */
  const getWarehousePositionOptionList = async (warehouseId: string) => {
    getWarehousePositionOptionListLoaing.value = true
    const res = await storePositionPageSelect(
      getWarehousePositionOptionListParam.value,
      warehouseId,
    )
    warehousePositionOptionList.value = res.data.data.rows
    getWarehousePositionOptionListLoaing.value = false
  }

  return {
    warehousePositionOptionList,
    getWarehousePositionOptionListParam,
    getWarehousePositionOptionListLoaing,
    getWarehousePositionOptionList,
  }
}

/**
 * 获取产品库存
 */
export const useGetGoodStock = () => {
  // 获取库存加载
  const getGoodStockLoading = ref(false)

  // 获取库存请求参数
  const getGoodStockParam = ref<StorageAPIRequest.GetWarehouseGoodStock>({
    goodsId: '',
    goodsSpecId: '',
    storeId: '',
    storePositionId: '',
    type: 1,
  })

  // 产品库存
  const goodStock = ref<WarehouseGoodStock>({
    price: 0,
    systemStock: 0,
    id: '',
  })

  /**
   * 获取仓库上皮库存
   */
  const getWarehouseGoodStock = async () => {
    getGoodStockLoading.value = true
    const res = await getGoodsStock(getGoodStockParam.value)
    goodStock.value = res.data.data.row
    getGoodStockLoading.value = false
  }

  return {
    goodStock,
    getGoodStockParam,
    getGoodStockLoading,
    getWarehouseGoodStock,
  }
}

/**
 * 获取在库的条码列表
 */
export const useGetInWarehouseSnCodeList = () => {
  // 获取列表加载
  const getSnCodeListLoading = ref(false)

  // 获取条码列表请求参数
  const getSnCodeListParam = ref<CodeAPIRequest.GetInWarehouseSnCodeList>({
    goodsId: '',
    goodsSpecId: '',
    keyword: '',
    limit: 50,
    page: 1,
    storeId: '',
    storePositionId: '',
    type: 1,
  })

  // 列表总条数
  const listTotal = ref(0)

  // 条码列表
  const snCodeList = ref<Array<WarehouseOutInWarehouseSnCode>>([])

  /**
   * 获取条码列表
   */
  const getSnCodeList = async () => {
    getSnCodeListLoading.value = true
    const res = await getInStoreCodeList(getSnCodeListParam.value)
    listTotal.value = Number(res.data.data.total)
    snCodeList.value = res.data.data.rows
    getSnCodeListLoading.value = false
  }

  return {
    listTotal,
    snCodeList,
    getSnCodeListParam,
    getSnCodeListLoading,
    getSnCodeList,
  }
}

/**
 * 获取出库详情
 */
export const useGetWarehouseOutDetail = () => {
  // 获取详情加载
  const getDetailLoading = ref(false)

  // 出库单详情
  const detail = ref<WarehouseOutDetailBeforeCheck>({
    cancelEmp: '',
    cancelRemark: '',
    cancelTime: '',
    createEmp: '',
    createTime: '',
    estimateTime: '',
    id: '',
    logisticsName: '',
    logisticsNo: '',
    logisticsRemark: '',
    outboundNo: '',
    receiptAddrType: 1,
    receiptAddress: '',
    receiptArea: '',
    receiptCity: '',
    receiptName: '',
    receiptNo: '',
    receiptPhone: '',
    receiptProvince: '',
    receiptTown: '',
    rejectEmp: '',
    rejectRemark: '',
    rejectTime: '',
    relationId: '',
    remark: '',
    reviewEmp: '',
    reviewRemark: '',
    reviewTime: '',
    source: 1,
    status: 1,
    storePositionId: '',
    storeType: 0,
    type: '',
    typeId: '',
    updateEmp: '',
    updateTime: '',
  })

  /**
   * 获取详情
   * @param id - 出库单id
   */
  const getDetail = async (id: string) => {
    getDetailLoading.value = true
    const res = await getOutboundDetail(id)
    detail.value = res.data.data.row
    getDetailLoading.value = false
  }

  return {
    detail,
    getDetailLoading,
    getDetail,
  }
}

/**
 * 根据出库单id获取物料列表
 */
export const useGetMaterialListByWarehouseOutId = () => {
  // 获取物料列表加载
  const getMaterialListLoading = ref(false)

  // 物料列表
  const materialList = ref<Array<WarehouseOutMaterial>>([])

  /**
   * 获取物料列表
   * @param id - 出库单id
   */
  const getMaterialList = async (id: string) => {
    getMaterialListLoading.value = true
    const res = await getOutboundMaterial(id)
    materialList.value = res.data.data.row
    getMaterialListLoading.value = false
  }

  return {
    materialList,
    getMaterialListLoading,
    getMaterialList,
  }
}

/**
 * 驳回出库单
 */
export const useRejectWarheouseOut = () => {
  // 驳回表单
  const form = ref({
    remark: '',
  })

  // 驳回加载
  const rejectLoading = ref(false)

  /**
   * 驳回
   * @param id - 出库单id
   */
  const reject = async (id: string) => {
    rejectLoading.value = true
    const res = await rejectOutbound(form.value, id)
    rejectLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value.remark = ''
  }

  return {
    form,
    rejectLoading,
    reject,
    resetForm,
  }
}

/**
 * 获取出库列表
 */
export const useGetWarehouseOutList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref<StorageAPIRequest.GetWarehouseOutList>({
    dateType: '',
    endTime: '',
    limit: 50,
    page: 1,
    search: '',
    source: '',
    startTime: '',
    status: '',
    type: '',
  })

  // 列表
  const list = ref<Array<WarehouseOut>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await outboundList(getListParam.value)
    getListLoading.value = false
    if (res.data.code === 20000) {
      list.value = res.data.data.rows
      listTotal.value = Number(res.data.data.total)
      return ''
    }
    return res.data.message
  }

  return {
    list,
    listTotal,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 出库单反写
 */
export const useWarehouseOutWriteBack = () => {
  // 反写加载
  const writeBackLoading = ref(false)

  /**
   * 反写
   * @param {Array<string>} idList - 出库单id集合
   */
  const writeBack = async (idList: Array<string>) => {
    writeBackLoading.value = true
    const res = await warehouseOutWriteBack(idList)
    writeBackLoading.value = false
    return res.data
  }

  return {
    writeBackLoading,
    writeBack,
  }
}
